package com.gc.frontend.service.impl;

import com.gc.notify.GovcarSubject;
import com.gc.common.dto.MessageDTO;
import com.gc.common.entity.Message;
import com.gc.common.mapper.MessageMapper;
import com.gc.frontend.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 瑱彬 on 2015/4/30.
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Resource(name = "messageSubject")
    private GovcarSubject messageSubject;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return this.messageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Message record) {
        record.setMessageStatus(Message.UNREAD);
        int count = this.messageMapper.insert(record);
        Map messageEntity = new HashMap<>();
        messageEntity.put("message", record);
        messageSubject.setMessage(messageEntity);
        messageSubject.notifyObserver();
        return count;
    }

    @Override
    public int insertSelective(Message record) {
        record.setMessageStatus(Message.UNREAD);
        int count = this.messageMapper.insertSelective(record);
        Map messageEntity = new HashMap<>();
        messageEntity.put("message", record);
        messageSubject.setMessage(messageEntity);
        messageSubject.notifyObserver();
        return count;
    }

    @Override
    public Message selectByPrimaryKey(Integer id) {
        return this.messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Message record) {
        return this.messageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Message record) {
        return this.messageMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Message> selectByDTO(MessageDTO dto) {
        return this.messageMapper.selectByDTO(dto);
    }

    @Override
    public int deleteInIds(String ids) {
        return this.deleteInIds(ids);
    }

    @Override
    public void markRead(int messageId) {
        Message message = new Message();
        message.setMessageId(messageId);
        message.setMessageStatus(Message.READ);
        updateByPrimaryKeySelective(message);
    }

    @Override
    public List<Map<String, Object>> selectUnread() {
        return this.messageMapper.selectUnread();
    }

}
